package com.mxpio.erp.equipment.job;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentRepair;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Classname TaskReleaseJob
 * @Description
 */
@Slf4j
@Configurable
@EnableScheduling
@Component
public class EquipmentRepairJob {

    public static final Integer APPLY = 10;
    /**
     * 待维修
     */
    public static final Integer SEND = 20;
    /**
     * 维修中
     */
    public static final Integer REPAIR = 30;
    /**
     * 暂停中
     */
    public static final Integer STOP = 35;
    /**
     * 待验收
     */
    public static final Integer FINISH = 40;
    /**
     * 验收完成
     */
    public static final Integer CHECKED = 50;

    /**
     * 取消/终止
     */
    public static final Integer CANCLE = 90;


    //提前几天
    public static final Integer offSetDay = -3;
    /**
     * 超过两天没有审核的数据变为自动审核
     */
    //@Scheduled(cron = "0 0 2 * * ? ")
    @Transactional
    public void executeEvaluateJob(){
        String threeDaysBefore = DateUtil.format(DateUtil.offsetDay(new Date(),offSetDay), "yyyy-MM-dd");
        Criteria dateCriteria = Criteria.create();
        dateCriteria.addCriterion("updateTime",Operator.LT,threeDaysBefore);
        List<EquipmentRepair> repairList = JpaUtil.linq(EquipmentRepair.class).equal("orderStatus", FINISH).where(dateCriteria).list();
        if (repairList.size()>0){
            for (EquipmentRepair repair:repairList){
                repair.setOrderStatus(CHECKED);
                repair.setEvaluateDate(new Date());
                repair.setEvaluateStar(5);
                repair.setIsUsable(Integer.parseInt(CommonEnums.YesNo.YES.getCode()));
                repair.setEvaluateContent("系统默认评价");
                JpaUtil.update(repair);
            }
        }
    }
}
